//
//  YQLHomeTableViewController.swift
//  YQL_WeiBo
//
//  Created by apple on 2016/11/12.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

let cellID: String = "statusCellID"

class YQLHomeTableViewController: YQLBascTableViewController {
    
    var homeViewModel:YQLHomeViewModel = YQLHomeViewModel()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        //判断是否登录,如果登录就修改更新视图
        if !YQLOAuthViewModel.shared.DidLogIn {
            
            VisitorView.setupVisitorView(title: "关注一些人,回到这里看看有什么惊喜", imageName: "visitordiscover_feed_image_smallicon", isHome: true)
            return
        }
        //如果登录了就对tableView进行设置加载
        //通过调用自定义的UIBarButtonItem的分类中的便利构造方法添加navigationItem的右侧按钮
        let rightScanButton = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: #selector(clickScanButton))
        
        self.navigationItem.rightBarButtonItem = rightScanButton
        
        //界面搭建
        setupUI()
        
        //通过ViewModel获取网络数据
        homeViewModel.loadData { (finish) in
            self.tableView.reloadData()
        }
        
    }

    //扫一扫按钮的点击事件
    @objc private func clickScanButton() {
        print(#function,"点击了扫一扫按钮")
        
        self.navigationController?.pushViewController(YQLScanViewController(), animated: true)
    }
    
    func setupUI() {
        //注册Cell
        //self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
        let nib = UINib.init(nibName: "YQLStatusCell", bundle: nil)
        self.tableView.register(nib, forCellReuseIdentifier: cellID)
        tableView.rowHeight = 500
        
    }
    
    
    // MARK: - Table view data source
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homeViewModel.modelArr.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! YQLStatusCell
        let model = homeViewModel.modelArr[indexPath.row]
        cell.cellViewModel = model
        
        return cell
    }


}
